package com.hontech.kaltaapp.ui.dialog

import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.TextView
import com.hontech.kaltaapp.R
import com.hontech.kaltaapp.utils.AppTask
import kotlinx.coroutines.delay


class ProgDialog : DraggableWindow(
    ViewGroup.LayoutParams.WRAP_CONTENT,
    ViewGroup.LayoutParams.WRAP_CONTENT,
    R.layout.dialog_prog,
    false
) {

    private val btnClose = view.findViewById<Button>(R.id.id_prog_btn_close)
    private val tvTitle = view.findViewById<TextView>(R.id.id_prog_tv_title)
    private val tvMsg = view.findViewById<TextView>(R.id.id_prog_tv_msg)
    private val img = view.findViewById<ImageView>(R.id.id_prog_img)
    private val prog = view.findViewById<ProgressBar>(R.id.id_prog_prog)

    init {
        btnClose.setOnClickListener { dismiss() }
    }

    fun prog() {
        img.visibility = View.INVISIBLE
        prog.visibility = View.VISIBLE
    }

    fun error() {
        showCenter()
        img.visibility = View.VISIBLE
        prog.visibility = View.INVISIBLE
        img.setImageResource(R.drawable.ic_err)
    }

    fun success() {
        img.visibility = View.VISIBLE
        prog.visibility = View.INVISIBLE
        img.setImageResource(R.drawable.ic_succ)
    }

    fun setTitle(title: String) {
        tvTitle.text = title
    }

    fun setMsg(msg: String) {
        tvMsg.text = msg
    }

    suspend fun succ(msg: String) {
        success()
        setMsg(msg)
        delay(800)
        dismiss()
    }

    fun err(e: Exception) {
        error()
        setMsg(e.message!!)
    }
}

fun showProg(title: String, msg: String): ProgDialog {
    val dialog = ProgDialog()
    dialog.prog()
    dialog.setTitle(title)
    dialog.setMsg(msg)
    dialog.showCenter()
    return dialog
}

suspend fun runProgMsg(title: String, msg: String, cb: suspend () -> String) {
    val dialog = ProgDialog()
    dialog.prog()
    dialog.setTitle(title)
    dialog.setMsg(msg)
    dialog.showCenter()
    try {
        val text = cb()
        dialog.setMsg(text)
        dialog.success()
    } catch (e: Exception) {
        e.printStackTrace()
        dialog.setMsg("${msg}=>${e.message}")
        dialog.error()
    }
}

suspend fun <T> runProg(title: String, msg: String, cb: suspend (ProgDialog) -> T?): T? {

    val dialog = ProgDialog()
    dialog.prog()
    dialog.setTitle(title)
    dialog.setMsg(msg)
    dialog.showCenter()
    return try {
        val result = cb(dialog)
        dialog.setMsg("${msg}=>成功")
        dialog.success()
        delay(700)
        dialog.dismiss()
        result
    } catch (e: Exception) {
        e.printStackTrace()
        dialog.setMsg("${msg}=>${e.message}")
        dialog.error()
        null
    }
}

fun showErr(title: String, e: Exception) {
    val dialog = ProgDialog()
    dialog.error()
    dialog.setTitle(title)
    dialog.setMsg(e.message!!)
    dialog.showCenter()
}

fun showSucc(msg: String) {
    val dialog = ProgDialog()
    dialog.success()
    dialog.setMsg("成功")
    dialog.setTitle(msg)
    dialog.showCenter()
    AppTask.postDelayed({
        dialog.dismiss()
    }, 800)
}





